define(["jquery", "easy-admin", 'miniTab'], function ($, ea, miniTab) {

    var init = {
        index_url: '/manage/member/miniapp/my/index',
        default_url: '/manage/member/miniapp/my/default',
    };

    var Controller = {
        index: function () {
            ea.request.get(
                {
                    url: init.index_url,
                    data: {},
                }, function (res) {
                    var list = res.data.data.list;

                    var html = '';
                    list.forEach(v => {
                        // var button = res.data.data.default_miniapp_dir === v.miniapp.dir ? '<button type="button" class="layui-btn layui-btn-xs layui-btn-danger" data-dir="manage">取消默认应用</button>' : '<button type="button" class="layui-btn layui-btn-xs layui-btn-normal" data-dir="' + v.miniapp.dir + '">设为默认应用</button>';
                        var button = '';
                        html += '<tr><td>' + v.id + '</td><td>' + v.appname + '</td><td>' + v.service_id + '</td><td>' + v.create_time + '</td><td>' + button + '</td></tr>';
                    });
                    $('#my-miniapp').html(html);
                }
            );

            $('body').off('click', '[data-dir]').on('click', '[data-dir]', function (event) {
                var miniapp_dir = $(this).attr('data-dir');
                ea.request.post({
                    url: init.default_url,
                    data: {
                        miniapp_dir: miniapp_dir,
                    },
                }, function (res) {
                    if(miniapp_dir === 'manage'){//取消默认应用
                        ea.msg.success("取消成功", function () {
                            window.location.reload();
                        }, 500)
                    }else {//设置默认应用
                        ea.msg.confirm("设置成功,是否前往管理该应用?", function () {
                            parent.window.location = '/manage/member/index';
                        }, function () {
                            window.location.reload();
                        })
                    }
                },);

            })

            ea.listen();
        },
        add: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
    };
    return Controller;
});